package me.shurufa.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;
import me.shurufa.R;
import me.shurufa.activities.BookCommentActivity;
import me.shurufa.activities.UserInfoActivity;
import me.shurufa.model.Comment;
import me.shurufa.model.CommentListResponse;
import me.shurufa.net.NetworkConstants;
import me.shurufa.net.RequestServerTask;
import me.shurufa.utils.Constants;
import me.shurufa.utils.Global;
import me.shurufa.utils.HttpUtil;
import me.shurufa.utils.Utils;
import me.shurufa.widget.CircleImageView;

/**
 * 点击书架上的书跳转到图书信息中针对我的评论的Fragment
 *
 * @FileName: me.shurufa.fragments.MyAboutBookCommentFragment.java
 * @author: kHRYSTAL
 * @email: 723526676@qq.com
 * @date: 2016-02-04 18:20
 */
public class MyAboutBookCommentFragment extends BaseFragment implements View.OnClickListener {


    @Bind(R.id.divider)
    View divider;
    @Bind(R.id.comment_container)
    LinearLayout commentContainer;
    @Bind(R.id.empty_view)
    RelativeLayout emptyView;
    @Bind(R.id.add_comment)
    RelativeLayout addComment;
    @Bind(R.id.comment_root)
    LinearLayout commentRoot;
    private long bookId;
    private String bookTitle;
    private LayoutInflater inflater;

    public static MyAboutBookCommentFragment newInstance(long bookId, String bookTitle) {
        MyAboutBookCommentFragment fragment = new MyAboutBookCommentFragment();
        Bundle args = new Bundle();
        args.putLong(Constants.ARG_BOOK_ID, bookId);
        args.putString(Constants.ARG_BOOK_TITLE, bookTitle);
        fragment.setArguments(args);
        return fragment;
    }

    public static MyAboutBookCommentFragment newInstance(long bookId) {
        MyAboutBookCommentFragment fragment = new MyAboutBookCommentFragment();
        Bundle args = new Bundle();
        args.putLong(Constants.ARG_BOOK_ID, bookId);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            bookId = getArguments().getLong(Constants.ARG_BOOK_ID);
            bookTitle = getArguments().getString(Constants.ARG_BOOK_TITLE);
        }
        inflater = LayoutInflater.from(getActivity());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_my_about_comment, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    protected void initUI(View view) {
        super.initUI(view);
        addComment.setOnClickListener(this);
        showEmptyView();
        loadData();
    }

    private void loadData() {
        new RequestServerTask<CommentListResponse>(CommentListResponse.class) {

            @Override
            protected String requestServer() {
                Map<String, Object> postParams = new HashMap<String, Object>();
                postParams.put("userId", Global.getCurrentUid());
                postParams.put("bookId", bookId);
                postParams.put("pageSize", 3);
                return HttpUtil.post(NetworkConstants.COMMENT_GET_BY_USER_AND_BOOK, postParams);
            }

            @Override
            protected void onSuccess(CommentListResponse result) {
                if (result.data != null)
                    initCommentLayout(result.data);
            }
        }.start();
    }

    /**
     * 动态添加布局至container
     */
    private void initCommentLayout(Comment[] comments) {
        if (comments.length > 0) {
            hideEmptyView();
            for (int i = 0; i < comments.length; i++) {
                final Comment comment = comments[i];
                if (comment != null && comment.user_info != null) {
                    View view = inflater.inflate(R.layout.list_item_my_comment, commentContainer, false);
                    TextView commentDate = (TextView) view.findViewById(R.id.comment_date);
                    if (comment.dateline!=0L)
                        commentDate.setText(Utils.getDatetimeStr(comment.dateline*1000L));
                    TextView content = (TextView) view.findViewById(R.id.comment_comment);
                    if (!TextUtils.isEmpty(comment.content))
                        content.setText(comment.content);
                    if (i == comments.length - 1) {
                        View divider = view.findViewById(R.id.comment_divider);
                        divider.setVisibility(View.GONE);
                    }

                    commentContainer.addView(view);
                }
            }
        } else {
            showEmptyView();
        }
    }


    public void refresh() {
        commentContainer.removeAllViews();
        loadData();
    }

    public void hideEmptyView() {
        emptyView.setVisibility(View.GONE);
    }

    public void showEmptyView() {
        emptyView.setVisibility(View.VISIBLE);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.add_comment:
                EventBus.getDefault().post(new Intent(Constants.ACTION_BOOKINFO_ADD_COMMENT));
                break;
        }
    }

    @Override
    public void onEventMainThread(Intent intent) {
        super.onEventMainThread(intent);
        if (intent.getAction().equals(Constants.ACTION_BOOKINFO_COMMENT_REFRESH)) {
            refresh();
        }
    }
}
